<?php

namespace app\service;

//玩家角色碎片服务层

use app\model\StaffFr;
use app\model\UserStaffFr;

class UserStaffFrService extends BaseService
{

    //添加玩家角色
    public function add_user_staff_fr($user_id, $staff_fr_id, $num): array
    {
        //查询角色是否存在
        $staff_fr = StaffFr::where("id", $staff_fr_id)->find();
        if (!$staff_fr) {
            return $this->returnFail("角色碎片不存在");
        }

        //查询玩家是否已经拥有该角色碎片
        $user_staff_fr = UserStaffFr::where("user_id", $user_id)->where("staff_fr_id", $staff_fr_id)->find();
        if (!$user_staff_fr) {
            $user_staff_fr = new UserStaffFr();
            $user_staff_fr->user_id = $user_id;
            $user_staff_fr->staff_fr_id = $staff_fr_id;
        }
        $user_staff_fr->num = $user_staff_fr->num + $num;
        $user_staff_fr->save();

        return $this->returnSuccess("添加成功");
    }

}